<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cart"></i> 产品管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>拼团管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box"
           style="margin-bottom:50px">
        <el-col :span="12"
                style="margin-bottom:20px;">
          <el-button type="danger"
                     icon="el-icon-delete"
                     class="handle-del mr10"
                     @click="delAllSelection">批量删除</el-button>
          <el-button type="primary"
                     icon="el-icon-circle-plus-outline"
                     class="handle-del mr10"
                     @click="handleAdd">添加拼团</el-button>
          <el-button type="primary"
                     icon="el-icon-circle-plus-outline"
                     class="handle-del mr10"
                     @click="handleClass">编辑分类</el-button>
          <el-button type="primary"
                     icon="el-icon-lx-goods"
                     class="handle-del mr10"
                     @click="handleUGroupClick">U趣拼</el-button>
        </el-col>
        <el-col :span="12"
                style="width:100%;display:flex;justify-content: center;margin-bottom:20px;align-items: center;">
          <el-select v-model="query.sort"
                     placeholder="排序"
                     class="handle-select mr10">
            <el-option key="1"
                       label="正序"
                       value="1"></el-option>
            <el-option key="2"
                       label="倒序"
                       value="2"></el-option>
          </el-select>

          <el-select v-model="query.g_status"
                     placeholder="拼团状态"
                     class="handle-select mr10">
            <el-option label="全部拼团状态"
                       value=""></el-option>
            <el-option key="1"
                       label="未开始"
                       value="1"></el-option>
            <el-option key="2"
                       label="已开始"
                       value="2"></el-option>
            <el-option key="3"
                       label="已结束"
                       value="3"></el-option>
            <el-option key="4"
                       label="已解散"
                       value="4"></el-option>
            <el-option key="5"
                       label="已失败"
                       value="5"></el-option>
          </el-select>

          <el-select v-model="query.status"
                     placeholder="状态"
                     class="handle-select mr10">
            <el-option label="全部状态"
                       value=""></el-option>
            <el-option key="1"
                       label="正常"
                       value="1"></el-option>
            <el-option key="2"
                       label="禁用"
                       value="2"></el-option>
          </el-select>
          <el-input v-model="query.name"
                    placeholder="商品名称"
                    class="handle-input mr10"
                    style="width:150px"></el-input>
          <el-button type="primary"
                     icon="el-icon-search"
                     @click="handleSearch">搜索</el-button>
        </el-col>
      </div>
      <el-table :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection"
                         width="55"
                         align="center"></el-table-column>
        <el-table-column prop="id"
                         label="ID"
                         width="70"
                         sortable
                         align="center"></el-table-column>
        <el-table-column prop="name"
                         label="名称"></el-table-column>
        <el-table-column label="分类"
                         align="center">
          <template slot-scope="scope">
            {{scope.row.info.group_class}}
          </template>
        </el-table-column>
        <el-table-column label="列表图"
                         align="center">
          <template slot-scope="scope">
            <el-image class="table-td-thumb"
                      :src="scope.row.img_url"
                      :preview-src-list="[scope.row.img_url]"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="order_num"
                         label="已拼团人数"></el-table-column>
        <el-table-column label="限制拼团人数"
                         align="center">
          <template slot-scope="scope">
            {{scope.row.info.group_population}}
          </template>
        </el-table-column>
        <el-table-column label="限制拼中人数"
                         align="center">
          <template slot-scope="scope">
            {{scope.row.info.prize_population}}
          </template>
        </el-table-column>
        <el-table-column label="红包金额"
                         align="center">
          <template slot-scope="scope">
            {{scope.row.info.bonus}}
          </template>
        </el-table-column>
        <el-table-column label="赠送金额"
                         align="center">
          <template slot-scope="scope">
            {{scope.row.info.give_money}}
          </template>
        </el-table-column>
        <el-table-column label="赠送钻石"
                         align="center">
          <template slot-scope="scope">
            {{scope.row.diamond}}
          </template>
        </el-table-column>
        <el-table-column label="是否自动创建"
                         align="center">
          <template slot-scope="scope">
            {{scope.row.info.is_auto==1?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column label="时间方式"
                         align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.info.time_type==1?'success':''">{{scope.row.info.time_type==1?'限制时间':'不限时间'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="拼团时间"
                         align="center">
          <template slot-scope="scope">

            {{scope.row.info.time_type==1?scope.row.info.group_start_time+' ~ '+scope.row.info.group_end_time:'不限'}}
          </template>
        </el-table-column>
        <el-table-column label="佣金方式"
                         align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.info.brokerage_type==1?'success':''">{{scope.row.info.brokerage_type==1?'比例':'固定'}}</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="一级佣金"
                         align="center">
          <template slot-scope="scope">
            {{scope.row.info.brokerage_type==1?scope.row.info.stair_money+'%':'￥'+scope.row.info.stair_money}}
          </template>
        </el-table-column>
        <el-table-column label="二级佣金"
                         align="center">
          <template slot-scope="scope">
            {{scope.row.info.brokerage_type==1?scope.row.info.second_money+'%':'￥'+scope.row.info.second_money}}
          </template>
        </el-table-column>
        <el-table-column prop="sales"
                         label="销量"></el-table-column>
        <el-table-column prop="virtual_sales"
                         label="虚拟销量"></el-table-column>
        <!-- <el-table-column label="是否包邮">
          <template slot-scope="scope">
            {{scope.row.is_postage==1?'是':'否'}}
          </template>
        </el-table-column> -->
        <el-table-column label="邮费">
          <template slot-scope="scope">
            <span v-if="scope.row.is_postage==1">
              <span style="color:#1DBE36">包邮</span>
            </span>
            <span v-else>
              {{scope.row.postage}}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="stock"
                         label="库存"></el-table-column>
        <el-table-column prop="price"
                         label="价格"></el-table-column>
        <el-table-column prop="original_price"
                         label="原价"></el-table-column>
        <el-table-column prop="factory_price"
                         label="进货价"></el-table-column>
        <!-- <el-table-column prop="origin"
                         label="产地"></el-table-column> -->
        <el-table-column label="排序"
                         prop="sort"
                         sortable="custom"></el-table-column>

        <el-table-column label="状态"
                         align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status==1?'success':'danger'">{{scope.row.status==1?'正常':'禁用'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="拼团状态"
                         align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.info.status==0">未开始</el-tag>
            <el-tag v-else-if="scope.row.info.status==1"
                    type="success">已开始</el-tag>
            <el-tag v-else-if="scope.row.info.status==2"
                    type="danger">已结束</el-tag>
            <el-tag v-else-if="scope.row.info.status==3"
                    type="danger">已解散</el-tag>
            <el-tag v-else
                    type="danger">已失败</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="updated_at"
                         sortable
                         label="修改时间"></el-table-column> -->
        <el-table-column prop="created_at"
                         label="添加时间"></el-table-column>
        <el-table-column label="操作"
                         width="220"
                         fixed="right"
                         align="center">
          <template slot-scope="scope">

            <el-button v-if="scope.row.info.status<2"
                       type="text"
                       :icon="scope.row.info.is_auto==1?'el-icon-lx-unlock':'el-icon-lx-lock'"
                       @click="autoGoodsGroup(scope.$index,scope.row.id)">{{ scope.row.info.is_auto==1?'关闭自动创建':'开启自动创建' }}</el-button>
            <el-button v-if="scope.row.info.status<2"
                       type="text"
                       icon="el-icon-lx-exit"
                       @click="dissolveGoodsGroup(scope.row.id)">解散</el-button>
            <el-button type="text"
                       icon="el-icon-delete"
                       class="red"
                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background
                       layout="total, prev, pager, next"
                       :current-page="query.page"
                       :page-size="query.pageSize"
                       :total="pageTotal"
                       @current-change="handlePageChange"></el-pagination>
      </div>
    </div>

    <!-- 编辑分类 -->
    <el-dialog v-dialogDrag
               title="编辑分类"
               :visible.sync="classVisible"
               width="30%">
      <el-form ref="addData"
               :model="classGroup"
               label-width="70px">
        <el-form-item label="分类一">
          <el-input v-model="classGroup.class1.name"
                    maxlength="30"
                    minlength="2"></el-input>
        </el-form-item>
        <el-form-item label="分类二">
          <el-input v-model="classGroup.class2.name"
                    maxlength="30"
                    minlength="2"></el-input>
        </el-form-item>
        <el-form-item label="分类三">
          <el-input v-model="classGroup.class3.name"
                    maxlength="30"
                    type="text"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="classVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="saveGroupClass">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'goodsGoup',
  data () {
    return {
      query: {
        status: '',
        sort: '',
        name: '',
        page: 1,
        pageSize: 10,
        g_status: ''
      },
      cascader: [],
      tableData: [],
      multipleSelection: [],
      delList: [],
      classGroup: { class1: { name: '', value: 1 }, class2: { name: '', value: 2 }, class3: { name: '', value: 3 } },
      addVisible: false,
      classVisible: false,
      pageTotal: 0,
      idx: -1,
      id: -1,
      getList: function () {
        let that = this;
        this.api.getGoodsGroup(this.query, function (data) {
          for (let i in data.list) {
            data.list[i].status = data.list[i].status + '';
          }
          that.tableData = data.list;
          that.pageTotal = data.pageTotal;
        });
      },
      delGoods: function (ids, msg) {
        let that = this;
        if (!ids || ids.length < 1) {
          this.$message.error('请选择删除的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.delGoodsGroup(ids, function (response) {
            that.multipleSelection = [];
            that.getList();
            that.$message.success(msg);
          });
        }).catch(() => { });
      }
    };
  },
  $route: {
    handler: function (val, oldVal) {
      if (val.name == 'goods-group') {
        this.getList();
      }
    },
    deep: true
  },
  created () {
    this.getList();
    let that = this;
    this.api.getGroupClass(function (res) {
      that.classGroup = res.info;
    });

  },
  methods: {
    dissolveGoodsGroup (id) {
      let that = this;
      // 二次确认解散
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        that.api.dissolveGoodsGroup(id, function (res) {
          that.$message.success(res.msg);
          that.getList();
        });
      }).catch(() => { });

    },

    //弹出分类编辑
    handleClass () {
      this.classVisible = true;
    },

    /**
     * 自动创建
     */
    autoGoodsGroup (index, id) {
      let that = this;
      that.api.autoCreation(id, function (res) {
        that.tableData[index].info.is_auto = that.tableData[index].info.is_auto == 1 ? 0 : 1;
        that.$message.success(res.msg);
      });
    },

    //保存分类
    saveGroupClass () {
      let that = this;
      that.api.editGroupClass(that.classGroup, function () {
        that.classVisible = false;
        that.$message.success('保存成功');
      });
    },

    // 触发搜索按钮
    handleSearch () {
      this.$set(this.query, 'page', 1);
      this.getList();
    },
    // 删除操作
    handleDelete (index, row) {
      this.delGoods([row.id], '删除成功');
    },
    // 多选操作
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    delAllSelection () {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
        ids.push(this.multipleSelection[i].id);
      }
      this.delGoods(ids, `删除了${str}`);
    },
    /***
     * 添加操作
     */
    handleAdd () {
      this.$router.push({
        name: `goods-group-info`
      });
    },
    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    },
    //U趣拼
    handleUGroupClick () {
      this.$router.push({
        name: `u-group`
      });
    },
  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}
.form-item {
    width: 285px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.previews {
    display: flex;
    flex-flow: nowrap row;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: fixed;
    z-index: 9999999999;
    background: rgba(0, 0, 0, 0.3);
    justify-content: center;
    align-items: center;
}
.previews > img {
    max-width: 90%;
    max-height: 90%;
}
.el-image-viewer__close {
    color: #ffffff;
}
</style>
